package cn.ctodb.divination.service;

import cn.ctodb.divination.domain.Sancai;
import cn.ctodb.divination.repository.SancaiRepository;
import cn.ctodb.divination.repository.search.SancaiSearchRepository;
import cn.ctodb.divination.service.dto.SancaiDTO;
import cn.ctodb.divination.service.mapper.SancaiMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing Sancai.
 */
@Service
@Transactional
public class SancaiService {

    private final Logger log = LoggerFactory.getLogger(SancaiService.class);

    private final SancaiRepository sancaiRepository;

    private final SancaiMapper sancaiMapper;

    private final SancaiSearchRepository sancaiSearchRepository;

    public SancaiService(SancaiRepository sancaiRepository, SancaiMapper sancaiMapper, SancaiSearchRepository sancaiSearchRepository) {
        this.sancaiRepository = sancaiRepository;
        this.sancaiMapper = sancaiMapper;
        this.sancaiSearchRepository = sancaiSearchRepository;
    }

    /**
     * Save a sancai.
     *
     * @param sancaiDTO the entity to save
     * @return the persisted entity
     */
    public SancaiDTO save(SancaiDTO sancaiDTO) {
        log.debug("Request to save Sancai : {}", sancaiDTO);
        Sancai sancai = sancaiMapper.sancaiDTOToSancai(sancaiDTO);
        sancai = sancaiRepository.save(sancai);
        SancaiDTO result = sancaiMapper.sancaiToSancaiDTO(sancai);
        sancaiSearchRepository.save(sancai);
        return result;
    }

    /**
     *  Get all the sancais.
     *
     *  @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<SancaiDTO> findAll() {
        log.debug("Request to get all Sancais");
        List<SancaiDTO> result = sancaiRepository.findAll().stream()
            .map(sancaiMapper::sancaiToSancaiDTO)
            .collect(Collectors.toCollection(LinkedList::new));

        return result;
    }

    /**
     *  Get one sancai by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true)
    public SancaiDTO findOne(Long id) {
        log.debug("Request to get Sancai : {}", id);
        Sancai sancai = sancaiRepository.findOne(id);
        SancaiDTO sancaiDTO = sancaiMapper.sancaiToSancaiDTO(sancai);
        return sancaiDTO;
    }

    /**
     *  Delete the  sancai by id.
     *
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete Sancai : {}", id);
        sancaiRepository.delete(id);
        sancaiSearchRepository.delete(id);
    }

    /**
     * Search for the sancai corresponding to the query.
     *
     *  @param query the query of the search
     *  @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<SancaiDTO> search(String query) {
        log.debug("Request to search Sancais for query {}", query);
        return StreamSupport
            .stream(sancaiSearchRepository.search(queryStringQuery(query)).spliterator(), false)
            .map(sancaiMapper::sancaiToSancaiDTO)
            .collect(Collectors.toList());
    }

    @Transactional(readOnly = true)
    public SancaiDTO findByTitle(String title){
        return sancaiMapper.sancaiToSancaiDTO(sancaiRepository.findByTitle(title));
    }
}
